<template>
  <div class="login-container">
    <!-- 系统标题 -->
    <div class="system-title">
      <div class="logo">🧠📚</div>
      <h1>智能图书推荐系统</h1>
    </div>

    <!-- 登录卡片 -->
    <div class="login-card">
      <div class="left-section">
        <div></div>
        <!-- 智能推荐插图 -->
        <div class="ai-recommendation">
          <div class="recommendation-visual">
            <div class="book-stack">
              <div class="book red"></div>
              <div class="book blue"></div>
              <div class="book green"></div>
            </div>
            <div class="ai-icon">🧠</div>
            <div class="recommended-books">
              <div class="book small purple"></div>
              <div class="book small orange"></div>
              <div class="book small teal"></div>
            </div>
          </div>
        </div>
      </div>

      <div class="right-section">
        <h2>快登录吧</h2>
        <el-form
          ref="loginForm"
          :model="formData"
          :rules="rules"
          label-position="top"
          @submit.native.prevent="handleLogin"
        >
          <el-form-item label="账号" prop="username">
            <el-input
              v-model="formData.username"
              placeholder="请输入账号"
              :disabled="isLoading"
              show-word-limit
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              v-model="formData.password"
              type="password"
              placeholder="请输入密码"
              :disabled="isLoading"
              maxlength="50"
              show-word-limit
              show-password
            ></el-input>
          </el-form-item>
          <div v-if="errorMessage" class="error-message">
            {{ errorMessage }}
          </div>
          <el-form-item>
            <el-button
              type="primary"
              class="login-button"
              :loading="isLoading"
              native-type="submit"
            >
              立即登录
            </el-button>
          </el-form-item>
          <div class="register-link">
            没有账号？<a href="#" @click.prevent="gotoRegister">点击注册</a>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { login } from '@/api/login'
import { loginRules } from '@/utils/validator'

export default {
  name: 'LoginView',
  data() {
    return {
      // 表单数据
      formData: {
        username: '',
        password: ''
      },
      rules: loginRules,
      isLoading: false,
      errorMessage: ''
    }
  },
  methods: {
    // 处理登录
    async handleLogin() {
      this.$refs.loginForm.validate(async (valid) => {
        if (valid) {
          try {
            this.isLoading = true
            this.errorMessage = ''
            const res = await login({
              username: this.formData.username,
              password: this.formData.password
            })
            if (res.code === '200') {
              this.$message.success('登录成功')
              // 将用户信息持久化存储到localStorage
              localStorage.setItem('userInfo', JSON.stringify(res.data))
              // 根据不同角色跳转到不同页面
              const currentRoute =
                res.data.role === 'admin'
                  ? '/admin/dashboard'
                  : '/user/recommend'
              this.$router.push(currentRoute)
            }
          } catch (error) {
            console.log(error, 'errror')
          } finally {
            this.isLoading = false
          }
        }
      })
    },

    gotoRegister() {
      // 跳转到注册页面
      this.$router.push('/register')
    }
  },
  created() {}
}
</script>

<style scoped lang="less">
:deep(.el-form--label-top .el-form-item__label) {
  padding: 0 !important;
}
.login-container {
  width: 100vw;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
  box-sizing: border-box;
}

.system-title {
  text-align: center;
  margin-bottom: 40px;
  color: white;
}

.logo {
  font-size: 48px;
  margin-bottom: 10px;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.05);
  }
  100% {
    transform: scale(1);
  }
}

.system-title h1 {
  color: white;
  font-size: 28px;
  font-weight: 600;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.login-card {
  display: flex;
  background: white;
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  max-width: 900px;
  width: 100%;
}

.left-section {
  flex: 1;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 60px 40px;
  position: relative;
  overflow: hidden;
}

.left-section::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="none"><path d="M0,0 L100,0 L100,100 Z" fill="rgba(255,255,255,0.1)"/></svg>');
  background-size: cover;
}

/* 智能推荐插图样式 */
.ai-recommendation {
  width: 100%;
  max-width: 280px;
  position: relative;
  z-index: 1;
}

.recommendation-visual {
  display: flex;
  align-items: center;
  gap: 30px;
  justify-content: center;
}

.book-stack {
  display: flex;
  gap: 5px;
  transform: rotate(-5deg);
}

.ai-icon {
  font-size: 60px;
  animation: float 3s ease-in-out infinite;
}

@keyframes float {
  0% {
    transform: translateY(0px);
  }
  50% {
    transform: translateY(-10px);
  }
  100% {
    transform: translateY(0px);
  }
}

.recommended-books {
  display: flex;
  gap: 5px;
  transform: rotate(5deg);
}

.book {
  width: 40px;
  height: 120px;
  border-radius: 4px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

.book.red {
  background: linear-gradient(90deg, #ff4b5c 0%, #ff3742 100%);
  box-shadow: 0 4px 8px rgba(255, 75, 92, 0.3);
}

.book.blue {
  background: linear-gradient(90deg, #4a90e2 0%, #357abd 100%);
  box-shadow: 0 4px 8px rgba(74, 144, 226, 0.3);
}

.book.green {
  background: linear-gradient(90deg, #2ecc71 0%, #27ae60 100%);
  box-shadow: 0 4px 8px rgba(46, 204, 113, 0.3);
}

.book.purple {
  background: linear-gradient(90deg, #9b59b6 0%, #8e44ad 100%);
  box-shadow: 0 4px 8px rgba(155, 89, 182, 0.3);
}

.book.orange {
  background: linear-gradient(90deg, #f39c12 0%, #e67e22 100%);
  box-shadow: 0 4px 8px rgba(243, 156, 18, 0.3);
}

.book.teal {
  background: linear-gradient(90deg, #1abc9c 0%, #16a085 100%);
  box-shadow: 0 4px 8px rgba(26, 188, 156, 0.3);
}

/* 小型图书样式 */
.book.small {
  width: 30px;
  height: 70px;
}

.right-section {
  flex: 1;
  padding: 60px 40px;
  display: flex;
  flex-direction: column;
}

.right-section h2 {
  color: #333;
  font-size: 24px;
  margin-bottom: 30px;
  font-weight: 600;
}

/* 表单样式 */
.form-group {
  margin-bottom: 20px;
  position: relative;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
  color: #333;
  font-size: 14px;
}

.form-input {
  width: 100%;
  height: 40px;
  padding: 0 12px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 14px;
  transition: all 0.3s ease;
  box-sizing: border-box;
  outline: none;
}

.form-input:focus {
  border-color: #4a90e2;
  box-shadow: 0 0 0 3px rgba(74, 144, 226, 0.1);
}

.form-input:disabled {
  background-color: #f5f5f5;
  cursor: not-allowed;
  opacity: 0.7;
}

/* 表单验证错误样式 */
.field-error {
  color: #ff4b5c;
  font-size: 12px;
  margin-top: 5px;
  position: absolute;
  bottom: -20px;
  left: 0;
}

/* 全局错误信息样式 */
.error-message {
  color: #ff4b5c;
  font-size: 14px;
  margin-bottom: 15px;
  padding: 10px 12px;
  background-color: #fff5f5;
  border: 1px solid #ffd6d6;
  border-radius: 6px;
  text-align: center;
  animation: shake 0.5s ease-in-out;
}

/* 错误抖动动画 */
@keyframes shake {
  0%,
  100% {
    transform: translateX(0);
  }
  10%,
  30%,
  50%,
  70%,
  90% {
    transform: translateX(-3px);
  }
  20%,
  40%,
  60%,
  80% {
    transform: translateX(3px);
  }
}

/* 登录按钮样式 */
.login-button {
  width: 100%;
  height: 40px;
  background: linear-gradient(135deg, #4a90e2 0%, #357abd 100%);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-bottom: 20px;
}

.login-button:hover:not(:disabled) {
  background: linear-gradient(135deg, #5a99e5 0%, #4586d4 100%);
}

.login-button:active:not(:disabled) {
  background: linear-gradient(135deg, #357abd 0%, #2a68a8 100%);
}

.login-button:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

/* 测试账号信息样式 */
.test-account {
  background-color: #f5f7fa;
  border: 1px solid #e4e7ed;
  border-radius: 8px;
  padding: 15px;
  margin-bottom: 20px;
}

.test-account h4 {
  color: #333;
  font-size: 16px;
  margin-bottom: 10px;
  font-weight: 600;
}

.test-account p {
  color: #666;
  font-size: 14px;
  margin-bottom: 5px;
}

.register-link {
  text-align: center;
  color: #666;
  font-size: 14px;
  margin-bottom: 20px;
}

.register-link a {
  color: #4a90e2;
  text-decoration: none;
  transition: color 0.3s ease;
}

.register-link a:hover {
  text-decoration: underline;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-card {
    flex-direction: column;
    max-width: 400px;
  }

  .left-section {
    padding: 40px;
    min-height: 200px;
  }

  .right-section {
    padding: 40px 30px;
  }

  .test-account p {
    font-size: 13px;
  }
}
</style>
